package com.cencat.route.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cencat.common.result.Result;
import com.cencat.route.entity.Delivery;
import com.cencat.route.service.DeliveryService;
import com.cencat.route.vo.DeliveryQueryVO;
import com.cencat.route.vo.DeliveryStatisticsVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 配送管理控制器
 * 
 * @author cencat
 * @since 2024-01-01
 */
@Slf4j
@RestController
@RequestMapping("/api/route/delivery")
@RequiredArgsConstructor
@Tag(name = "配送管理", description = "配送信息的增删改查和业务操作")
public class DeliveryController {

    private final DeliveryService deliveryService;

    @GetMapping("/page")
    @Operation(summary = "分页查询配送", description = "根据条件分页查询配送信息")
    public Result<Page<Delivery>> getDeliveryPage(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Long size,
            @Parameter(description = "查询条件") DeliveryQueryVO query) {
        try {
            Page<Delivery> page = new Page<>(current, size);
            Page<Delivery> result = deliveryService.selectDeliveryPage(page, query);
            return Result.success(result);
        } catch (Exception e) {
            log.error("分页查询配送失败: {}", e.getMessage(), e);
            return Result.error("分页查询配送失败: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    @Operation(summary = "查询配送列表", description = "根据条件查询配送列表")
    public Result<List<Delivery>> getDeliveryList(@Parameter(description = "查询条件") DeliveryQueryVO query) {
        try {
            List<Delivery> result = deliveryService.selectDeliveryList(query);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询配送列表失败: {}", e.getMessage(), e);
            return Result.error("查询配送列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询配送", description = "根据配送ID查询详细信息")
    public Result<Delivery> getDeliveryById(@Parameter(description = "配送ID") @PathVariable Long id) {
        try {
            Delivery delivery = deliveryService.getById(id);
            if (delivery == null) {
                return Result.error("配送不存在");
            }
            return Result.success(delivery);
        } catch (Exception e) {
            log.error("查询配送详情失败: {}", e.getMessage(), e);
            return Result.error("查询配送详情失败: " + e.getMessage());
        }
    }

    @PostMapping
    @Operation(summary = "新增配送", description = "创建新的配送信息")
    public Result<Boolean> createDelivery(@Parameter(description = "配送信息") @Valid @RequestBody Delivery delivery) {
        try {
            boolean result = deliveryService.save(delivery);
            return result ? Result.success(true) : Result.error("新增配送失败");
        } catch (Exception e) {
            log.error("新增配送失败: {}", e.getMessage(), e);
            return Result.error("新增配送失败: " + e.getMessage());
        }
    }

    @PutMapping
    @Operation(summary = "更新配送", description = "更新配送信息")
    public Result<Boolean> updateDelivery(@Parameter(description = "配送信息") @Valid @RequestBody Delivery delivery) {
        try {
            boolean result = deliveryService.updateById(delivery);
            return result ? Result.success(true) : Result.error("更新配送失败");
        } catch (Exception e) {
            log.error("更新配送失败: {}", e.getMessage(), e);
            return Result.error("更新配送失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除配送", description = "根据ID删除配送")
    public Result<Boolean> deleteDelivery(@Parameter(description = "配送ID") @PathVariable Long id) {
        try {
            boolean result = deliveryService.removeById(id);
            return result ? Result.success(true) : Result.error("删除配送失败");
        } catch (Exception e) {
            log.error("删除配送失败: {}", e.getMessage(), e);
            return Result.error("删除配送失败: " + e.getMessage());
        }
    }

    @PutMapping("/batch/status")
    @Operation(summary = "批量更新配送状态", description = "批量更新配送状态")
    public Result<Boolean> batchUpdateDeliveryStatus(
            @Parameter(description = "配送ID列表") @RequestBody List<Long> ids,
            @Parameter(description = "状态") @RequestParam String status,
            @Parameter(description = "更新人ID") @RequestParam Long updatedBy) {
        try {
            boolean result = deliveryService.batchUpdateDeliveryStatus(ids, status, updatedBy);
            return result ? Result.success(true) : Result.error("批量更新配送状态失败");
        } catch (Exception e) {
            log.error("批量更新配送状态失败: {}", e.getMessage(), e);
            return Result.error("批量更新配送状态失败: " + e.getMessage());
        }
    }

    @PutMapping("/batch/priority")
    @Operation(summary = "批量更新优先级", description = "批量更新配送优先级")
    public Result<Boolean> batchUpdatePriority(
            @Parameter(description = "配送ID列表") @RequestBody List<Long> ids,
            @Parameter(description = "优先级") @RequestParam Integer priority,
            @Parameter(description = "更新人ID") @RequestParam Long updatedBy) {
        try {
            boolean result = deliveryService.batchUpdatePriority(ids, priority, updatedBy);
            return result ? Result.success(true) : Result.error("批量更新优先级失败");
        } catch (Exception e) {
            log.error("批量更新优先级失败: {}", e.getMessage(), e);
            return Result.error("批量更新优先级失败: " + e.getMessage());
        }
    }

    @PutMapping("/batch/assign-courier")
    @Operation(summary = "批量分配配送员", description = "批量为配送分配配送员")
    public Result<Boolean> batchAssignCourier(
            @Parameter(description = "配送ID列表") @RequestBody List<Long> ids,
            @Parameter(description = "配送员ID") @RequestParam Long courierId,
            @Parameter(description = "分配人ID") @RequestParam Long assignedBy) {
        try {
            boolean result = deliveryService.batchAssignCourier(ids, courierId, assignedBy);
            return result ? Result.success(true) : Result.error("批量分配配送员失败");
        } catch (Exception e) {
            log.error("批量分配配送员失败: {}", e.getMessage(), e);
            return Result.error("批量分配配送员失败: " + e.getMessage());
        }
    }

    @PutMapping("/batch/update-fee")
    @Operation(summary = "批量更新配送费用", description = "批量更新配送费用")
    public Result<Boolean> batchUpdateDeliveryFee(
            @Parameter(description = "配送ID列表") @RequestBody List<Long> ids,
            @Parameter(description = "配送费用") @RequestParam Double deliveryFee,
            @Parameter(description = "更新人ID") @RequestParam Long updatedBy) {
        try {
            boolean result = deliveryService.batchUpdateDeliveryFee(ids, deliveryFee, updatedBy);
            return result ? Result.success(true) : Result.error("批量更新配送费用失败");
        } catch (Exception e) {
            log.error("批量更新配送费用失败: {}", e.getMessage(), e);
            return Result.error("批量更新配送费用失败: " + e.getMessage());
        }
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取配送统计信息", description = "获取配送相关统计数据")
    public Result<DeliveryStatisticsVO> getDeliveryStatistics() {
        try {
            DeliveryStatisticsVO result = deliveryService.getDeliveryStatistics();
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取配送统计信息失败: {}", e.getMessage(), e);
            return Result.error("获取配送统计信息失败: " + e.getMessage());
        }
    }

    @PostMapping("/smart-match")
    @Operation(summary = "智能匹配配送", description = "根据条件智能匹配合适的配送")
    public Result<String> smartMatchDeliveries(
            @Parameter(description = "匹配条件") @RequestParam String matchCriteria,
            @Parameter(description = "最大匹配数量") @RequestParam(defaultValue = "10") Integer maxMatches) {
        try {
            String result = deliveryService.smartMatchDeliveries(matchCriteria, maxMatches);
            return Result.success(result);
        } catch (Exception e) {
            log.error("智能匹配配送失败: {}", e.getMessage(), e);
            return Result.error("智能匹配配送失败: " + e.getMessage());
        }
    }

    @GetMapping("/pending")
    @Operation(summary = "获取待配送列表", description = "获取待配送的订单列表")
    public Result<List<Delivery>> getPendingDeliveries(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "50") Integer limit) {
        try {
            List<Delivery> result = deliveryService.getPendingDeliveries(limit);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取待配送列表失败: {}", e.getMessage(), e);
            return Result.error("获取待配送列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/in-progress")
    @Operation(summary = "获取配送中列表", description = "获取正在配送的订单列表")
    public Result<List<Delivery>> getInProgressDeliveries(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "50") Integer limit) {
        try {
            List<Delivery> result = deliveryService.getInProgressDeliveries(limit);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取配送中列表失败: {}", e.getMessage(), e);
            return Result.error("获取配送中列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/completed")
    @Operation(summary = "获取已完成配送列表", description = "获取已完成的配送列表")
    public Result<List<Delivery>> getCompletedDeliveries(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "50") Integer limit) {
        try {
            List<Delivery> result = deliveryService.getCompletedDeliveries(limit);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取已完成配送列表失败: {}", e.getMessage(), e);
            return Result.error("获取已完成配送列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/urgent")
    @Operation(summary = "获取紧急配送列表", description = "获取紧急的配送列表")
    public Result<List<Delivery>> getUrgentDeliveries(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "30") Integer limit) {
        try {
            List<Delivery> result = deliveryService.getUrgentDeliveries(limit);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取紧急配送列表失败: {}", e.getMessage(), e);
            return Result.error("获取紧急配送列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/high-priority")
    @Operation(summary = "获取高优先级配送列表", description = "获取高优先级的配送列表")
    public Result<List<Delivery>> getHighPriorityDeliveries(
            @Parameter(description = "最低优先级") @RequestParam(defaultValue = "8") Integer minPriority,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "30") Integer limit) {
        try {
            List<Delivery> result = deliveryService.getHighPriorityDeliveries(minPriority, limit);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取高优先级配送列表失败: {}", e.getMessage(), e);
            return Result.error("获取高优先级配送列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/overdue")
    @Operation(summary = "获取逾期配送列表", description = "获取逾期的配送列表")
    public Result<List<Delivery>> getOverdueDeliveries(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "30") Integer limit) {
        try {
            List<Delivery> result = deliveryService.getOverdueDeliveries(limit);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取逾期配送列表失败: {}", e.getMessage(), e);
            return Result.error("获取逾期配送列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/today")
    @Operation(summary = "获取今日配送列表", description = "获取今日的配送列表")
    public Result<List<Delivery>> getTodayDeliveries(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "100") Integer limit) {
        try {
            List<Delivery> result = deliveryService.getTodayDeliveries(limit);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取今日配送列表失败: {}", e.getMessage(), e);
            return Result.error("获取今日配送列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/by-courier/{courierId}")
    @Operation(summary = "根据配送员ID获取配送列表", description = "获取指定配送员的配送列表")
    public Result<List<Delivery>> getDeliveriesByCourier(
            @Parameter(description = "配送员ID") @PathVariable Long courierId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "50") Integer limit) {
        try {
            List<Delivery> result = deliveryService.getDeliveriesByCourier(courierId, limit);
            return Result.success(result);
        } catch (Exception e) {
            log.error("根据配送员ID获取配送列表失败: {}", e.getMessage(), e);
            return Result.error("根据配送员ID获取配送列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/by-merchant/{merchantId}")
    @Operation(summary = "根据商户ID获取配送列表", description = "获取指定商户的配送列表")
    public Result<List<Delivery>> getDeliveriesByMerchant(
            @Parameter(description = "商户ID") @PathVariable Long merchantId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "50") Integer limit) {
        try {
            List<Delivery> result = deliveryService.getDeliveriesByMerchant(merchantId, limit);
            return Result.success(result);
        } catch (Exception e) {
            log.error("根据商户ID获取配送列表失败: {}", e.getMessage(), e);
            return Result.error("根据商户ID获取配送列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/by-route/{routeId}")
    @Operation(summary = "根据路线ID获取配送列表", description = "获取指定路线的配送列表")
    public Result<List<Delivery>> getDeliveriesByRoute(
            @Parameter(description = "路线ID") @PathVariable Long routeId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "50") Integer limit) {
        try {
            List<Delivery> result = deliveryService.getDeliveriesByRoute(routeId, limit);
            return Result.success(result);
        } catch (Exception e) {
            log.error("根据路线ID获取配送列表失败: {}", e.getMessage(), e);
            return Result.error("根据路线ID获取配送列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/check/exists/{id}")
    @Operation(summary = "检查配送是否存在", description = "根据ID检查配送是否存在")
    public Result<Boolean> checkDeliveryExists(@Parameter(description = "配送ID") @PathVariable Long id) {
        try {
            boolean result = deliveryService.checkDeliveryExists(id);
            return Result.success(result);
        } catch (Exception e) {
            log.error("检查配送是否存在失败: {}", e.getMessage(), e);
            return Result.error("检查配送是否存在失败: " + e.getMessage());
        }
    }

    @GetMapping("/check/code")
    @Operation(summary = "检查配送编码是否存在", description = "检查配送编码是否已被使用")
    public Result<Boolean> checkDeliveryCodeExists(
            @Parameter(description = "配送编码") @RequestParam String deliveryCode,
            @Parameter(description = "排除的配送ID") @RequestParam(required = false) Long excludeId) {
        try {
            boolean result = deliveryService.checkDeliveryCodeExists(deliveryCode, excludeId);
            return Result.success(result);
        } catch (Exception e) {
            log.error("检查配送编码是否存在失败: {}", e.getMessage(), e);
            return Result.error("检查配送编码是否存在失败: " + e.getMessage());
        }
    }

    @PostMapping("/register")
    @Operation(summary = "注册配送", description = "注册新的配送任务")
    public Result<Boolean> registerDelivery(
            @Parameter(description = "配送信息") @Valid @RequestBody Delivery delivery,
            @Parameter(description = "注册人ID") @RequestParam Long registeredBy) {
        try {
            boolean result = deliveryService.registerDelivery(delivery, registeredBy);
            return result ? Result.success(true) : Result.error("注册配送失败");
        } catch (Exception e) {
            log.error("注册配送失败: {}", e.getMessage(), e);
            return Result.error("注册配送失败: " + e.getMessage());
        }
    }

    @PutMapping("/verify/{id}")
    @Operation(summary = "验证配送", description = "验证配送任务")
    public Result<Boolean> verifyDelivery(
            @Parameter(description = "配送ID") @PathVariable Long id,
            @Parameter(description = "验证结果") @RequestParam Boolean verificationResult,
            @Parameter(description = "验证备注") @RequestParam(required = false) String verificationNotes,
            @Parameter(description = "验证人ID") @RequestParam Long verifiedBy) {
        try {
            boolean result = deliveryService.verifyDelivery(id, verificationResult, verificationNotes, verifiedBy);
            return result ? Result.success(true) : Result.error("验证配送失败");
        } catch (Exception e) {
            log.error("验证配送失败: {}", e.getMessage(), e);
            return Result.error("验证配送失败: " + e.getMessage());
        }
    }

    @PutMapping("/activate/{id}")
    @Operation(summary = "激活配送", description = "激活配送任务")
    public Result<Boolean> activateDelivery(
            @Parameter(description = "配送ID") @PathVariable Long id,
            @Parameter(description = "激活人ID") @RequestParam Long activatedBy) {
        try {
            boolean result = deliveryService.activateDelivery(id, activatedBy);
            return result ? Result.success(true) : Result.error("激活配送失败");
        } catch (Exception e) {
            log.error("激活配送失败: {}", e.getMessage(), e);
            return Result.error("激活配送失败: " + e.getMessage());
        }
    }

    @PutMapping("/deactivate/{id}")
    @Operation(summary = "停用配送", description = "停用配送任务")
    public Result<Boolean> deactivateDelivery(
            @Parameter(description = "配送ID") @PathVariable Long id,
            @Parameter(description = "停用原因") @RequestParam String deactivationReason,
            @Parameter(description = "停用人ID") @RequestParam Long deactivatedBy) {
        try {
            boolean result = deliveryService.deactivateDelivery(id, deactivationReason, deactivatedBy);
            return result ? Result.success(true) : Result.error("停用配送失败");
        } catch (Exception e) {
            log.error("停用配送失败: {}", e.getMessage(), e);
            return Result.error("停用配送失败: " + e.getMessage());
        }
    }

    @PutMapping("/start/{id}")
    @Operation(summary = "开始配送", description = "开始执行配送任务")
    public Result<Boolean> startDelivery(
            @Parameter(description = "配送ID") @PathVariable Long id,
            @Parameter(description = "开始人ID") @RequestParam Long startedBy) {
        try {
            boolean result = deliveryService.startDelivery(id, startedBy);
            return result ? Result.success(true) : Result.error("开始配送失败");
        } catch (Exception e) {
            log.error("开始配送失败: {}", e.getMessage(), e);
            return Result.error("开始配送失败: " + e.getMessage());
        }
    }

    @PutMapping("/complete/{id}")
    @Operation(summary = "完成配送", description = "完成配送任务")
    public Result<Boolean> completeDelivery(
            @Parameter(description = "配送ID") @PathVariable Long id,
            @Parameter(description = "完成备注") @RequestParam(required = false) String completionNotes,
            @Parameter(description = "完成人ID") @RequestParam Long completedBy) {
        try {
            boolean result = deliveryService.completeDelivery(id, completionNotes, completedBy);
            return result ? Result.success(true) : Result.error("完成配送失败");
        } catch (Exception e) {
            log.error("完成配送失败: {}", e.getMessage(), e);
            return Result.error("完成配送失败: " + e.getMessage());
        }
    }

    @PutMapping("/cancel/{id}")
    @Operation(summary = "取消配送", description = "取消配送任务")
    public Result<Boolean> cancelDelivery(
            @Parameter(description = "配送ID") @PathVariable Long id,
            @Parameter(description = "取消原因") @RequestParam String cancelReason,
            @Parameter(description = "取消人ID") @RequestParam Long cancelledBy) {
        try {
            boolean result = deliveryService.cancelDelivery(id, cancelReason, cancelledBy);
            return result ? Result.success(true) : Result.error("取消配送失败");
        } catch (Exception e) {
            log.error("取消配送失败: {}", e.getMessage(), e);
            return Result.error("取消配送失败: " + e.getMessage());
        }
    }

    @PutMapping("/assign-courier/{id}")
    @Operation(summary = "分配配送员", description = "为配送分配配送员")
    public Result<Boolean> assignCourier(
            @Parameter(description = "配送ID") @PathVariable Long id,
            @Parameter(description = "配送员ID") @RequestParam Long courierId,
            @Parameter(description = "分配人ID") @RequestParam Long assignedBy) {
        try {
            boolean result = deliveryService.assignCourier(id, courierId, assignedBy);
            return result ? Result.success(true) : Result.error("分配配送员失败");
        } catch (Exception e) {
            log.error("分配配送员失败: {}", e.getMessage(), e);
            return Result.error("分配配送员失败: " + e.getMessage());
        }
    }

    @PutMapping("/update-fee/{id}")
    @Operation(summary = "更新配送费用", description = "更新配送任务的费用")
    public Result<Boolean> updateDeliveryFee(
            @Parameter(description = "配送ID") @PathVariable Long id,
            @Parameter(description = "配送费用") @RequestParam Double deliveryFee,
            @Parameter(description = "更新人ID") @RequestParam Long updatedBy) {
        try {
            boolean result = deliveryService.updateDeliveryFee(id, deliveryFee, updatedBy);
            return result ? Result.success(true) : Result.error("更新配送费用失败");
        } catch (Exception e) {
            log.error("更新配送费用失败: {}", e.getMessage(), e);
            return Result.error("更新配送费用失败: " + e.getMessage());
        }
    }

    @PutMapping("/update-progress/{id}")
    @Operation(summary = "更新配送进度", description = "更新配送任务的执行进度")
    public Result<Boolean> updateDeliveryProgress(
            @Parameter(description = "配送ID") @PathVariable Long id,
            @Parameter(description = "进度百分比") @RequestParam Integer progressPercentage,
            @Parameter(description = "进度描述") @RequestParam(required = false) String progressDescription,
            @Parameter(description = "更新人ID") @RequestParam Long updatedBy) {
        try {
            boolean result = deliveryService.updateDeliveryProgress(id, progressPercentage, progressDescription, updatedBy);
            return result ? Result.success(true) : Result.error("更新配送进度失败");
        } catch (Exception e) {
            log.error("更新配送进度失败: {}", e.getMessage(), e);
            return Result.error("更新配送进度失败: " + e.getMessage());
        }
    }

    @PostMapping("/auto-assign")
    @Operation(summary = "自动分配配送", description = "根据算法自动分配配送任务")
    public Result<Boolean> autoAssignDelivery(
            @Parameter(description = "配送ID") @RequestParam Long deliveryId,
            @Parameter(description = "分配策略") @RequestParam(defaultValue = "OPTIMAL") String assignmentStrategy,
            @Parameter(description = "分配人ID") @RequestParam Long assignedBy) {
        try {
            boolean result = deliveryService.autoAssignDelivery(deliveryId, assignmentStrategy, assignedBy);
            return result ? Result.success(true) : Result.error("自动分配配送失败");
        } catch (Exception e) {
            log.error("自动分配配送失败: {}", e.getMessage(), e);
            return Result.error("自动分配配送失败: " + e.getMessage());
        }
    }

    @PostMapping("/optimize-route/{id}")
    @Operation(summary = "优化配送路线", description = "优化配送任务的执行路线")
    public Result<Boolean> optimizeDeliveryRoute(
            @Parameter(description = "配送ID") @PathVariable Long id,
            @Parameter(description = "优化参数") @RequestParam String optimizationParams,
            @Parameter(description = "优化人ID") @RequestParam Long optimizedBy) {
        try {
            boolean result = deliveryService.optimizeDeliveryRoute(id, optimizationParams, optimizedBy);
            return result ? Result.success(true) : Result.error("优化配送路线失败");
        } catch (Exception e) {
            log.error("优化配送路线失败: {}", e.getMessage(), e);
            return Result.error("优化配送路线失败: " + e.getMessage());
        }
    }

    @PostMapping("/calculate-fee")
    @Operation(summary = "计算配送费用", description = "根据配送信息计算配送费用")
    public Result<Double> calculateDeliveryFee(
            @Parameter(description = "起点地址") @RequestParam String fromAddress,
            @Parameter(description = "终点地址") @RequestParam String toAddress,
            @Parameter(description = "货物重量(kg)") @RequestParam Double weight,
            @Parameter(description = "配送类型") @RequestParam String deliveryType) {
        try {
            Double result = deliveryService.calculateDeliveryFee(fromAddress, toAddress, weight, deliveryType);
            return Result.success(result);
        } catch (Exception e) {
            log.error("计算配送费用失败: {}", e.getMessage(), e);
            return Result.error("计算配送费用失败: " + e.getMessage());
        }
    }

    @PostMapping("/estimate-time")
    @Operation(summary = "预估配送时间", description = "根据配送信息预估配送时间")
    public Result<Integer> estimateDeliveryTime(
            @Parameter(description = "起点地址") @RequestParam String fromAddress,
            @Parameter(description = "终点地址") @RequestParam String toAddress,
            @Parameter(description = "配送类型") @RequestParam String deliveryType) {
        try {
            Integer result = deliveryService.estimateDeliveryTime(fromAddress, toAddress, deliveryType);
            return Result.success(result);
        } catch (Exception e) {
            log.error("预估配送时间失败: {}", e.getMessage(), e);
            return Result.error("预估配送时间失败: " + e.getMessage());
        }
    }

}